package com.aman.common;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aman.transformer.sample.R;

/**
 * Created by TedPark on 2016. 8. 30..
 */

/**
 * https://gist.github.com/liangzhitao/e57df3c3232ee446d464
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    protected int mOrientation;
    protected int padding;
    private int startpadding;
    private int endpadding;
    private int dividerHeight = 1;
    private Paint mPaddingPaint;
    private Paint mDividerPaint;
    private Context mContext;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    public GridSpacingItemDecoration(Context context, int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
        this.mContext = context;
        init();
    }

    public GridSpacingItemDecoration(Context context, int spanCount, int spacing, int dividerHeight, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
        this.mContext = context;
        this.dividerHeight = dividerHeight;
        init();
    }

    private void init() {
        padding = mContext.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
        updatePaddint();

        mPaddingPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaddingPaint.setColor(ContextCompat.getColor(mContext, android.R.color.white));
        mPaddingPaint.setStyle(Paint.Style.FILL);
        mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDividerPaint.setColor(ContextCompat.getColor(mContext, android.R.color.darker_gray));
        mDividerPaint.setStyle(Paint.Style.FILL);
    }

    private void updatePaddint() {
        startpadding = padding;
        endpadding = padding;
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child));
            final int bottom = top + dividerHeight;
            c.drawRect(left, top, left, bottom, mPaddingPaint);
            c.drawRect(right, top, right, bottom, mPaddingPaint);
            c.drawRect(left, top, right, bottom, mDividerPaint);
//            c.drawRect(left, top, left + startpadding, bottom, mPaddingPaint);
//            c.drawRect(right - endpadding, top, right, bottom, mPaddingPaint);
//            c.drawRect(left + startpadding, top, right - endpadding, bottom, mDividerPaint);
        }
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin + Math.round(ViewCompat.getTranslationX(child));
            final int right = left + dividerHeight;
            c.drawRect(left, top, right, top, mPaddingPaint);
            c.drawRect(left, bottom, right, bottom, mPaddingPaint);
            c.drawRect(left, top, right, bottom, mDividerPaint);
//            c.drawRect(left, top, right, top + startpadding, mPaddingPaint);
//            c.drawRect(left, bottom - endpadding, right, bottom, mPaddingPaint);
//            c.drawRect(left, top + startpadding, right, bottom - endpadding, mDividerPaint);
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mContext != null) {
            drawVertical(c, parent);
            drawHorizontal(c, parent);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        if (position >= 0) {
            int column = position % spanCount; // item column
            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        } else {
            outRect.left = 0;
            outRect.right = 0;
            outRect.top = 0;
            outRect.bottom = 0;
        }
    }
}
